checkLogin();
var worker = getCache(login_key);
var resend_list = [];
var order_id = GetQueryString('o_id');


//如果不存在order_id，下面的流程就不执行
function order() {
    if (!order_id) {
        return
    }
}
order();
var is_first = ''; //0:表示需要弹出提示框
var currenAddress = ''

/*页面渲染*/
var success = function (res) {
    console.log(res)
    var endAdress = res.data.info.buyer_address;
    is_first = res.data.is_first;
    var send_list = res.data;
    delete send_list.is_first;

    var html = template('order_detaile', send_list);
    document.getElementById('content-block').innerHTML = html;
    //关于高德地图
    gaodeMap(endAdress);

    /*if(worker.wid == 1){*/
        // var map = new AMap.Map("container");
        // map.plugin('AMap.Geolocation', function() {
        //     var geolocation = new AMap.Geolocation({
        //         // 是否使用高精度定位，默认：true
        //         enableHighAccuracy: true,
        //         // 设置定位超时时间，默认：无穷大
        //         timeout: 10000,
        //         // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
        //         buttonOffset: new AMap.Pixel(10, 20),
        //         //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
        //         zoomToAccuracy: true,
        //         //  定位按钮的排放位置,  RB表示右下
        //         buttonPosition: 'RB'
        //     })
        //
        //     geolocation.getCurrentPosition()
        //     AMap.event.addListener(geolocation, 'complete', onComplete)
        //     AMap.event.addListener(geolocation, 'error', onError)
        //
        //     function onComplete (data) {
        //
        //         var html = template('wid1Js', worker);
        //         document.getElementById('wid1').innerHTML = html;
        //         // data是具体的定位信息
        //         console.log(data);
        //         currenAddress = data.formattedAddress;
        //         //将两个地址放到缓存中
        //         var addressDetail = {'currenAddress':currenAddress,'endAdress':endAdress};
        //         setCache('address',addressDetail);
        //         $('#H5App').bind('click',function () {
        //             var url = 'newGaodeMap.html?currenAddress='+currenAddress +'&endAdress='+endAdress;
        //             url = encodeURI(url);
        //             window.location.href=url;
        //             /*redirect("create-order.html?mid=" + mid);*/
        //         })
        //
        //     }
        //
        //     function onError (data) {
        //         console.log(data)
        //         // 定位出错
        //     }
        // })

   /* }*/

     $('#call').bind('click', function () {
        var mobile = $(this).attr('data');
        window.location.href="tel:"+mobile;
     });
    //点击完成，删除当前订单返回上一个页面
    var recive_bill_money = 0;
    var recive_order_money = 0;
    var back = 0;
    // back是否需要回桶
     back = send_list.info.back;
    if (send_list.info.type == 1 && send_list.info.status == 10 ) {
        recive_order_money = send_list.info.order_amount;
    }
    if (typeof(send_list.info.bill_order) == 'object' && send_list.info.bill_order.money > 0 ) {
        recive_bill_money = send_list.info.bill_order.money;
    }
    $('#order-done').bind('click', function () {
        /*if(is_first == 1){

            $.alert_new('客户首次下单，请导入初始总水桶','提示','确定',function () {
                var href = 'customer-import.html?mid='+send_list.info.buyer_id +'&is_fisrt='+1 +'&o_id='+send_list.info.id;
                /!*var href = worker_bindwx+'?wid='+obj.wid+'&sid='+obj.sid+'&aid='+obj.aid;*!/
                redirect(href)
                // redirect('center.html');
            })


        }else {*/
            var success = function (res) {
                setTimeout(function () {
                    location.replace('order-list.html');
                },2000);
            };
            postData(worker_complete,
                {
                    id: order_id,
                    aid: worker.aid,
                    sid: worker.sid,
                    worker_id: worker.wid,
                    recive_bill_money: recive_bill_money,
                    recive_order_money: recive_order_money,
                    back: back
                }, success);

            // return;
        /*}*/
    });
    /*改派*/
    var success = function (res) {
        var values = [];
        resend_list = res.data;
        for (var i = 0; i < resend_list.length; i++) {
            values[i] = resend_list[i].name;
        }
        $(document).on('click', '.create-actions', function () {
            var buttons1 = [];
            for (var i = 0; i < resend_list.length; i++) {
                buttons1.push({
                    text: resend_list[i].name,
                    onClick: function () {
                        var worker_id = 0;
                        var worker_name = $(this)[0].text;
                        if (resend_list.length > 0) {
                            for (var j = 0; j < resend_list.length; j++) {
                                if (resend_list[j]['name'] == $(this)[0].text) {
                                    worker_id = resend_list[j].id;
                                }
                            }
                        }
                        $(".actions-modal-button").addClass("confirm-title-ok");
                        $(document).on('click', '.confirm-title-ok', function () {
                            $.confirm('您将要改派给' + '  ' + worker_name, function () {
                                var success = function () {
                                    if (res.status == 1) {
                                        $.toast('改派成功');
                                        setTimeout(function () {
                                            location.replace('order-list.html');
                                        }, 2000)
                                    }
                                };
                                getData(worker_order_resend, {
                                    wid: worker.wid,
                                    sid: worker.sid,
                                    aid: worker.aid,
                                    id: order_id,
                                    worker_id: worker_id,
                                    type: 1
                                }, success);
                            });
                        });
                    }
                });
            }
            var buttons2 = [
                {
                    text: '取消',
                    bg: 'danger'
                }
            ];
            var groups = [buttons1, buttons2];
            $.actions(groups);
        });
    }
    getData(worker_order_resend, {
        wid: worker.wid,
        sid: worker.sid,
        aid: worker.aid,
        id: order_id,
        worker_id: 0,
        type:0
    }, success);

    //回桶
    $('#tobucket').bind('click', function () {
        /*if(is_first == 1){
            //弹出对话框提示回桶
            $.alert_new('客户首次下单，请导入初始总水桶','提示','确定',function () {
                var href = 'customer-import.html?mid='+send_list.info.buyer_id +'&is_fisrt='+1 +'&o_id='+send_list.info.id;
                /!*var href = worker_bindwx+'?wid='+obj.wid+'&sid='+obj.sid+'&aid='+obj.aid;*!/
                redirect(href)
                return;
                // redirect('center.html');
            })
        }else {*/
        var list = send_list.info.order_goods;
        var cid = null;
        if(list != undefined && list != null && list != '' && list.length > 0){
            cid = list[0].bucket_category_id;
        }
            redirect('bucket.html?recive_bill_money=' + recive_bill_money +
                '&recive_order_money=' + recive_order_money +
                '&back=' + back + '&id=' + order_id +'&cid='+cid);
        /*}*/
    })

};
getData(worker_order_info, {wid: worker.wid, sid: worker.sid, aid: worker.aid, id: order_id, type: 2}, success);


function gaodeMap(endAddress){
    var url = 'https://restapi.amap.com/v3/geocode/geo?address='+endAddress+'&output=JSON&key=6bb6ea65c1a3881519a587cfae44d5a6';
    url = encodeURI(url);
    $.ajax({
        type:"get",
        url: url,
        dataType: "JSON",
        jsonp: 'callback',
        success: function (res) {

            var to = res.geocodes[0].location;
            console.log(to)
            $('#wid1').show();

            //点击导航 获取当前经纬度
            $('#H5App').bind('click',function () {
                // window.location.href ='https://uri.amap.com/navigation?from='+from+',startpoint&to='+to+',endpoint,midwaypoint&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0'
                var url = worker_wx_config;
                var success = function (res) {
                    wx.config({
                        debug: false,
                        appId: res.data.appId.toString(),
                        timestamp: res.data.timestamp,
                        nonceStr: res.data.nonceStr.toString(),
                        signature: res.data.signature.toString(),
                        jsApiList: [
                            // 所有要调用的 API 都要加到这个列表中
                            'checkJsApi',
                            'openLocation',
                            'getLocation'
                        ]
                    });
                    wx.ready(function () {
                        // 在这里调用 API
                        <!--通过checkJsApi判断当前客户端版本是否支持指定获取地理位置-->
                        wx.checkJsApi({
                            jsApiList: [
                                'getLocation'
                            ],
                            success: function (res) {
                                // alert(JSON.stringify(res));
                                // alert(JSON.stringify(res.checkResult.getLocation));
                                if (res.checkResult.getLocation == false) {
                                    // alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
                                    $.alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
                                    return;
                                }
                            }
                        });

                        <!--使用getLocation接口获取地理位置坐标-->
                        wx.getLocation({
                            success: function (res) {
                                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                                var speed = res.speed; // 速度，以米/每秒计
                                var accuracy = res.accuracy; // 位置精度

                                // alert('纬度位置：' + latitude + '经度位置：' + longitude);//弹出经纬度，就这样就获取到了用户的位置
                                var from = longitude+','+latitude;
                                var changeUrl = 'https://restapi.amap.com/v3/assistant/coordinate/convert?locations='+from+'&coordsys=gps&output=JSON&key=6bb6ea65c1a3881519a587cfae44d5a6';
                                $.ajax({
                                    type:"get",
                                    url: changeUrl,
                                    dataType: "JSON",
                                    jsonp: 'callback',
                                    success: function (res) {
                                        console.log(res)
                                        from = res.locations;
                                        window.location.href ='https://uri.amap.com/navigation?from='+from+',startpoint&to='+to+',endpoint,midwaypoint&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0'
                                    }
                                })


                            },
                            cancel: function (res) {
                                // alert('用户拒绝授权获取地理位置');
                                $.alert('用户拒绝授权获取地理位置');
                            }
                        });
                    });
                }
                var work_info = getCache(login_key);
                postData(url, {wid: work_info.wid, aid: work_info.aid, sid: work_info.sid,url:location.href.split('#')[0]}, success);
            })
        },
        error: function (res,reson) {
            $.alert('请求失败');
        }
    });
}




